package spem.activity.diagram.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

import spem.Activity;
import spem.SpemPackage;
import spem.activity.diagram.edit.parts.Activity2EditPart;
import spem.activity.diagram.edit.parts.ActivityEditPart;
import spem.activity.diagram.edit.parts.ActivityNameEditPart;
import spem.activity.diagram.edit.parts.ControlFlowEditPart;
import spem.activity.diagram.edit.parts.DecisionEditPart;
import spem.activity.diagram.edit.parts.LabelEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionNameEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionPerformsEditPart;
import spem.activity.diagram.edit.parts.Step2EditPart;
import spem.activity.diagram.edit.parts.StepName2EditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionInputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionNameEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionOutputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionTaskStepCompartmentEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionUsesEditPart;
import spem.activity.diagram.edit.parts.ToolEditPart;
import spem.activity.diagram.edit.parts.ToolNameEditPart;
import spem.activity.diagram.edit.parts.WorkProductDefinitionEditPart;
import spem.activity.diagram.edit.parts.WorkProductDefinitionNameEditPart;
import spem.activity.diagram.edit.parts.WrappingLabel2EditPart;
import spem.activity.diagram.edit.parts.WrappingLabel3EditPart;
import spem.activity.diagram.edit.parts.WrappingLabelEditPart;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class SpemVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "spemGraph.activity.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (ActivityEditPart.MODEL_ID.equals(view.getType())) {
				return ActivityEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return spem.activity.diagram.part.SpemVisualIDRegistry.getVisualID(view
				.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				SpemDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (SpemPackage.eINSTANCE.getActivity().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((Activity) domainElement)) {
			return ActivityEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = spem.activity.diagram.part.SpemVisualIDRegistry
				.getModelID(containerView);
		if (!ActivityEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (ActivityEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = spem.activity.diagram.part.SpemVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ActivityEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case ActivityEditPart.VISUAL_ID:
			if (SpemPackage.eINSTANCE.getWorkProductDefinition().isSuperTypeOf(
					domainElement.eClass())) {
				return WorkProductDefinitionEditPart.VISUAL_ID;
			}
			if (SpemPackage.eINSTANCE.getRoleDefinition().isSuperTypeOf(
					domainElement.eClass())) {
				return RoleDefinitionEditPart.VISUAL_ID;
			}
			if (SpemPackage.eINSTANCE.getActivity().isSuperTypeOf(
					domainElement.eClass())) {
				return Activity2EditPart.VISUAL_ID;
			}
			if (SpemPackage.eINSTANCE.getDecision().isSuperTypeOf(
					domainElement.eClass())) {
				return DecisionEditPart.VISUAL_ID;
			}
			if (SpemPackage.eINSTANCE.getTaskDefinition().isSuperTypeOf(
					domainElement.eClass())) {
				return TaskDefinitionEditPart.VISUAL_ID;
			}
			if (SpemPackage.eINSTANCE.getTool().isSuperTypeOf(
					domainElement.eClass())) {
				return ToolEditPart.VISUAL_ID;
			}
			break;
		case TaskDefinitionTaskStepCompartmentEditPart.VISUAL_ID:
			if (SpemPackage.eINSTANCE.getStep().isSuperTypeOf(
					domainElement.eClass())) {
				return Step2EditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = spem.activity.diagram.part.SpemVisualIDRegistry
				.getModelID(containerView);
		if (!ActivityEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (ActivityEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = spem.activity.diagram.part.SpemVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ActivityEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case ActivityEditPart.VISUAL_ID:
			if (WorkProductDefinitionEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RoleDefinitionEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Activity2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DecisionEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (TaskDefinitionEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ToolEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case WorkProductDefinitionEditPart.VISUAL_ID:
			if (WorkProductDefinitionNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RoleDefinitionEditPart.VISUAL_ID:
			if (RoleDefinitionNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Activity2EditPart.VISUAL_ID:
			if (ActivityNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TaskDefinitionEditPart.VISUAL_ID:
			if (TaskDefinitionNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (TaskDefinitionTaskStepCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ToolEditPart.VISUAL_ID:
			if (ToolNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Step2EditPart.VISUAL_ID:
			if (StepName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TaskDefinitionTaskStepCompartmentEditPart.VISUAL_ID:
			if (Step2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RoleDefinitionPerformsEditPart.VISUAL_ID:
			if (WrappingLabelEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TaskDefinitionInputEditPart.VISUAL_ID:
			if (LabelEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TaskDefinitionUsesEditPart.VISUAL_ID:
			if (WrappingLabel2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TaskDefinitionOutputEditPart.VISUAL_ID:
			if (WrappingLabel3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (SpemPackage.eINSTANCE.getControlFlow().isSuperTypeOf(
				domainElement.eClass())) {
			return ControlFlowEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(Activity element) {
		return true;
	}

}
